/*
 * @lc app=leetcode.cn id=504 lang=cpp
 *
 * [504] 七进制数
 *
 * https://leetcode.cn/problems/base-7/description/
 *
 * algorithms
 * Easy (52.13%)
 * Likes:    179
 * Dislikes: 0
 * Total Accepted:    74K
 * Total Submissions: 142.1K
 * Testcase Example:  '100'
 *
 * 给定一个整数 num，将其转化为 7 进制，并以字符串形式输出。
 *
 *
 *
 * 示例 1:
 *
 *
 * 输入: num = 100
 * 输出: "202"
 *
 *
 * 示例 2:
 *
 *
 * 输入: num = -7
 * 输出: "-10"
 *
 *
 *
 *
 * 提示：
 *
 *
 * -10^7 <= num <= 10^7
 *
 *
 */

// @lc code=start
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
    string convertToBase7(int num) {
        string ans;
        int flag = 0;
        if (num == 0) {
            return "0";
        }
        if (num < 0) {
            num = -num;
            flag = 1;
        }
        while (num > 0) {
            ans = to_string(num % 7) + ans;
            num /= 7;
        }
        if (flag) {
            ans = "-" + ans;
        }
        return ans;
    }
};
// @lc code=end
